// pages/settlementPage/settlementPage.js
Page({
    data: {
        cartSetList: [],
        payShow: false,
        payLoading: false,
        obj: null,
        textColorData: '#000000',
        list: [],
        backgroundData: 'transparent',
        // 下面为支付参数
        goods: [],
        down_from: 1, //默认立即购买来的，2------购物车
        pay_price: '', //支付金额
        pay_jf: '',
        address_id: '',
        isPayActive: null, //1.----》微信支付方式
        zhjf: '', //支付积分
        wxPay: false,
        xxPay: false,
    },

    onLoad(options) {
        let that = this;
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.on('acceptDataFromOpenerPage', function (data) {
            let setList = data.data.map(item => {
                return {
                    id: item.goods_id,
                    num: item.num,
                };
            });
            that.setData({
                cartSetList: setList,
            });
            that.request({
                url: 'user/my/order/payMessage',
                data: {
                    goods: setList,
                },
            }).then(res => {
                that.setData({
                    list: res.data,
                });
            });
        });
    },
    onShow() {
        this.getDefault();
        this.getPersonJifen();
        this.getPayType();
    },

    // 获取用户积分
    getPersonJifen() {
        this.request({
            url: 'user/money/msg',
        }).then(res => {
            console.log(res);
            this.setData({
                zhjf: res.data.jf,
            });
        });
    },

    tabPayType(e) {
        // console.log();
        this.setData({
            isPayActive: e.currentTarget.dataset.paynum,
        });
    },
    getPayType() {
        this.request({
            url: 'common/payConfig',
        }).then(res => {
            console.log('支付方式', res.data.wx_pay);
            this.setData({
                wxPay: res.data.wx_pay,
                xxPay: res.data.xx_pay,
            });
        });
    },
    // 获取默认收货地址
    getDefault() {
        this.request({
            url: 'user/my/address2/default',
        }).then(res => {
            this.setData({
                obj: res.data,
                address_id: res.data.id,
            });
        });
    },

    onVisibleChange() {
        this.setData({
            payShow: false,
        });
    },
    // 立即支付
    onClickPay() {
        if (!this.data.obj)
            return wx.showModal({
                title: '提示',
                content: '是否前往完善收货信息',
                confirmColor: '#F11224',
                complete: res => {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/address/address',
                        });
                    }
                },
            });
        const list = this.data.list;
        this.setData({
            payShow: true,
            pay_price: list.pay_price,
            goods: this.data.cartSetList,
            address_id: this.data.obj.id,
            pay_jf: list.dk_jf,
            down_from: 2,
        });
    },

    // 确认支付（微信支付）
    okPay() {
        if (this.data.isPayActive == null)
            return wx.showToast({
                title: '请选择支付方式',
                icon: 'none',
            });

        this.setData({
            payLoading: true,
        });
        // isPayActive==1(微信支付)
        if (this.data.isPayActive === 1) {
            this.request({
                url: 'user/my/order/pay',
                method: 'POST',
                data: {
                    goods: this.data.goods,
                    down_from: this.data.down_from,
                    pay_price: this.data.pay_price,
                    pay_jf: this.data.zhjf,
                    address_id: this.data.address_id,
                    pay_type: this.data.isPayActive,
                },
            })
                .then(async res => {
                    // console.log(res);
                    // if (res.data.pay_data == null) {
                    //     wx.showToast({
                    //         title: res.msg,
                    //     });
                    // }
                    const { timeStamp, paySign, signType, package: packageId, nonceStr } = res.data.pay_data;
                    wx.requestPayment({
                        timeStamp: String(timeStamp),
                        nonceStr,
                        package: packageId,
                        signType,
                        paySign,
                        success: async res => {
                            wx.showToast({
                                title: '支付成功',
                                icon: 'success',
                            });
                            await this.sleep(1000);
                            wx.navigateBack();
                        },
                        fail: async err => {
                            wx.showToast({
                                title: '支付失败',
                                icon: 'none',
                            });
                            await this.sleep(1000);
                            wx.navigateBack();
                        },
                    });

                    this.setData({
                        payLoading: false,
                        payShow: false,
                    });
                })
                .catch(err => {
                    console.log(err);
                    this.setData({
                        payLoading: false,
                        payShow: false,
                    });
                });
        } else {
            // 线下支付
            this.request({
                url: 'user/my/order/pay',
                method: 'POST',
                data: {
                    goods: this.data.goods,
                    down_from: this.data.down_from,
                    pay_price: this.data.pay_price,
                    pay_jf: this.data.zhjf,
                    address_id: this.data.address_id,
                    pay_type: this.data.isPayActive,
                },
            })
                .then(async res => {
                    if (res.data.pay_data == null) {
                        wx.showToast({
                            title: res.msg,
                            duration: 1800,
                        });
                        this.setData({
                            payLoading: false,
                            payShow: false,
                        });
                        await this.sleep(1600);
                        wx.navigateBack();
                    }
                })
                .catch(err => {
                    this.setData({
                        payLoading: false,
                        payShow: false,
                    });
                });
        }
    },

    onPageScroll(e) {
        if (e.scrollTop > 1) {
            this.setData({
                textColorData: '#ffffff',
                backgroundData: '#FFCCCC',
            });
        } else {
            this.setData({
                textColorData: '#000000',
                backgroundData: 'transparent',
            });
        }
    },
});
